#include "24Cxx.h"
#include "delay.h"
#include <stdio.h>
#include "drv_soft_iic.h"
#include "drv_pin.h"
#include "main.h"

static Drv_I2c_Bus at_i2c_bus;
static int8_t at_i2c_index = 0;


void at24cxxSleepEnter(void){
	hc32_pin_mode(AT_SCL_PIN,PIN_MODE_INPUT_PULLUP);
	hc32_pin_mode(AT_SDA_PIN,PIN_MODE_INPUT_PULLUP);
}

void at24cxxSleepExit(void){
	hc32_pin_mode(AT_SCL_PIN,PIN_MODE_OUTPUT_PULLUP);
	hc32_pin_mode(AT_SDA_PIN,PIN_MODE_OUTPUT_PULLUP);
}

uint8_t AT24CXX_ReadOneByte(uint16_t ReadAddr){				  
	uint8_t temp=0;		  	    																 
	at_i2c_bus.start(at_i2c_index);  
	if(EE_TYPE>AT24C16){
		at_i2c_bus.send_byte(at_i2c_index,0XA0);
		at_i2c_bus.wait_ask(at_i2c_index);
		at_i2c_bus.send_byte(at_i2c_index,ReadAddr>>8);
	}
	else {
		at_i2c_bus.send_byte(at_i2c_index,0XA0+((ReadAddr/256)<<1));   
	}
	at_i2c_bus.wait_ask(at_i2c_index);
	at_i2c_bus.send_byte(at_i2c_index,ReadAddr%256);
	at_i2c_bus.wait_ask(at_i2c_index);  
	at_i2c_bus.start(at_i2c_index);  	 	   
	at_i2c_bus.send_byte(at_i2c_index,0XA1);	   
	at_i2c_bus.wait_ask(at_i2c_index);	 
	temp=at_i2c_bus.read_byte(at_i2c_index,0);		   
	at_i2c_bus.stop(at_i2c_index);
  
	return temp;

}



void AT24CXX_WriteOneByte(uint16_t WriteAddr,uint8_t DataToWrite){
	at_i2c_bus.start(at_i2c_index); 
	if(EE_TYPE>AT24C16){
		at_i2c_bus.send_byte(at_i2c_index,0XA0);
		at_i2c_bus.wait_ask(at_i2c_index);
		at_i2c_bus.send_byte(at_i2c_index,WriteAddr>>8);
	}
	else{ 
		at_i2c_bus.send_byte(at_i2c_index,0XA0+((WriteAddr/256)<<1));
	}
	at_i2c_bus.wait_ask(at_i2c_index);   
	at_i2c_bus.send_byte(at_i2c_index,WriteAddr%256);
	at_i2c_bus.wait_ask(at_i2c_index);	 										  		   
	at_i2c_bus.send_byte(at_i2c_index,DataToWrite);					   
	at_i2c_bus.wait_ask(at_i2c_index);  		    	   
	at_i2c_bus.stop(at_i2c_index);
	Delay_ms(5);	 
}

uint8_t AT24CXX_Check(void){//读取最后一个字节
  uint8_t temp;
  temp=AT24CXX_ReadOneByte(AT24C08);
  if(temp==0XAA){
	  return 0;
  }else{
    AT24CXX_WriteOneByte(AT24C08,0XAA);
    temp=AT24CXX_ReadOneByte(AT24C08);	
    if(temp==0XAA){
		return 0;
	}
    if(temp!=0xAA){
		return 1;
	}
  }
  return 1;											  
}

void AT24CXX_Init(void){
	hc32_pin_mode(MCU_POWER_DVC,PIN_MODE_OUTPUT);
	hc32_pin_write(MCU_POWER_DVC,PIN_HIGH);
	
	at_i2c_index = drv_i2c_register(AT_SCL_PIN,AT_SDA_PIN,&at_i2c_bus);
	if(at_i2c_index < 0){
		printf("at24cxx i2c bus register failed,index = %d\n",at_i2c_index);
	}
	
	while(AT24CXX_Check()){
		printf("EEPROM init error!\n");
		Delay_ms(1000);
	}
}

void AT24CXX_Read(uint16_t ReadAddr,uint8_t *pBuffer,uint16_t NumToRead){
  while(NumToRead){
    *pBuffer++=AT24CXX_ReadOneByte(ReadAddr++);	
    NumToRead--;
  }
}  

void AT24CXX_Write(uint16_t WriteAddr,uint8_t *pBuffer,uint16_t NumToWrite){
  while(NumToWrite--){
    AT24CXX_WriteOneByte(WriteAddr,*pBuffer);
    WriteAddr++;
    pBuffer++;
  }
}


void AT24CXX_Format(void){
	uint8_t AT24CXXBuf[1023] = {0};
	AT24CXX_Write(0,(uint8_t *)&AT24CXXBuf,AT24C08-1);
}


void AT24CXX_Printf(void){
	uint8_t AT24CXXBuf[1023] = {0};
	AT24CXX_Read(0,(uint8_t *)&AT24CXXBuf,AT24C08-1);
	for(uint16_t i=0;i<1024;i++){
		printf("%02x ",AT24CXXBuf[i]);
	}	
	printf("\n");
}
